<?php
class Admin_Model extends CI_Model{

	public function __construct(){
		parent::__construct();
		$this->db 		= $this->load->database('default', TRUE);
	}
	public function getTypeSub($ref =null){
		$this->db->select('cat_type_sub_id, cat_type_sub_name');
		$this->db->where(array('cat_type_sub_delete_flag'=>0,'cat_type_id'=>$ref));
		$data = $this->db->get('cat_type_sub')->result_array();
		return $data;
	}
	public function getType(){
		$this->db->select('cat_type_id, cat_type_name');
		$this->db->where(array('cat_type_delete_flag'=>0));
		$data = $this->db->get('cat_type')->result_array();
		return $data;
	}
	public function saveCatSubProduct($ref = null){
		$this->db->insert('common_category', $ref);
		return $this->db->affected_rows();
	}
	public function saveCatSubProductMember($ref = null){
		$this->db->insert('member_category', $ref);
		return $this->db->affected_rows();
	}
	public function saveCate($ref = null){
		$this->db->insert('cat_type', $ref);
		return $this->db->affected_rows();
	}
	public function saveCateSub($ref = null){
		$this->db->insert('cat_type_sub', $ref);
		return $this->db->affected_rows();
	}
	public function searchCommonEdit($data =null,$limit = 10, $offset = 0){
		$this->db->select('cat_id, cat_name,images');
		$this->db->where(array('delete_flag'=>0));
		if (!empty($data['cat_name']))
			$this->db->like('cat_name',$data['cat_name'],'both');
		if (!empty($data['cat_color']))
			$this->db->where('cat_color',$data['cat_color'],'both');
		$this->db->where(array('cat_type_sub_id'=>$data['cat_type_sub_id']));
		$this->db->distinct();
		$data = $this->db->get('common_category',$limit, $offset)->result_array();
//		$sql = '';
//		$sql .= 'select cat_id, cat_name, images from common_category where ';
//		$sql .=' cat_name like % ? or';
//		$sql .=' cat_code like % ? or';
//		$sql .=' cat_description like % ? or';
//		$sql .=' cat_demension like % ? or';
//		$sql .=' cat_assembly like % ? or';
//		$sql .=' cat_material like % ? or';
//		$sql .=' cat_color like % ? or';
//		$sql .=' cat_type_sub_id = ? and';
//		$sql .=' delete_flag = 0';
//		$data = $this->db->query($sql,$dat)->result_array();
		return $data;
	}
	public function count_condition($data =null){
		$this->db->select('cat_id');
		$this->db->where(array('delete_flag'=>0));
		if (!empty($data['cat_name']))
			$this->db->like('cat_name',$data['cat_name'],'both');
		if (!empty($data['cat_color']))
			$this->db->where('cat_color',$data['cat_color'],'both');
		$this->db->where(array('cat_type_sub_id'=>$data['cat_type_sub_id']));
		$this->db->distinct();
		$data = $this->db->get('common_category')->result_array();
		return count($data);
	}
	public function searchMemberEdit($data = null,$limit = 10, $offset = 0){
		$this->db->select('cat_id, cat_name,images');
		$this->db->where(array('delete_flag'=>0));
		if (!empty($data['cat_name']))
			$this->db->like('cat_name',$data['cat_name'],'both');
		if (!empty($data['cat_color']))
			$this->db->where('cat_color',$data['cat_color'],'both');
		$this->db->where(array('cat_type_sub_id'=>$data['cat_type_sub_id']));
		$this->db->distinct();
		$data = $this->db->get('member_category',$limit, $offset)->result_array();
		return $data;
	}
	public function count_conditionMember($data =null){
		$this->db->select('cat_id');
		$this->db->where(array('delete_flag'=>0));
		if (!empty($data['cat_name']))
			$this->db->like('cat_name',$data['cat_name'],'both');
		if (!empty($data['cat_color']))
			$this->db->where('cat_color',$data['cat_color'],'both');
		$this->db->where(array('cat_type_sub_id'=>$data['cat_type_sub_id']));
		$this->db->distinct();
		$data = $this->db->get('member_category')->result_array();
		return count($data);
	}
	public function getImagesEdit(){
		$this->db->select('id,images, colums');
		$this->db->where('delete_flag',0);
		$this->db->order_by("colums",'asc'); 
		$data = $this->db->get('image_subbanner')->result_array();
		return $data;
	}
	public function updateImg($data = null){
		$id = $data['id'];
		$img = array('images'=>$data['images'],'delete_flag'=>$data['delete_flag']);
		$this->db->update('image_subbanner', $img, array('id' => $id));
		return $this->db->affected_rows();
	}
	public function searchPId($id=null){
		$this->db->select('cat_id,cat_name, cat_code,cat_price, cat_type_sub_sort_order,cat_description,cat_demension,cat_assembly,cat_material,cat_color, cat_size, cat_type_sub_id');
		$this->db->where(array('delete_flag'=>0,'cat_id'=>$id));
		$data = $this->db->get('common_category')->result_array();
		return $data;
	}
	public function searchPIdM($id=null){
		$this->db->select('cat_id,cat_name, cat_code,cat_price, cat_type_sub_sort_order,cat_description,cat_demension,cat_assembly,cat_material,cat_color, cat_size, cat_type_sub_id');
		$this->db->where(array('delete_flag'=>0,'cat_id'=>$id));
		$data = $this->db->get('member_category')->result_array();
		return $data;
	}
	public function updateProductId($data = null, $id=null){
		$this->db->update('common_category', $data, array('cat_id' => $id));
		return $this->db->affected_rows();
	}
	public function updateProductIdM($data = null, $id=null){
		$this->db->update('member_category', $data, array('cat_id' => $id));
		return $this->db->affected_rows();
	}
	public function delPId($id = null){
		$img = array('delete_flag'=>1);
		$this->db->update('common_category', $img, array('cat_id' => $id,'delete_flag'=>0));
		return $this->db->affected_rows();
	}
	public function delPIdM($id = null){
		$img = array('delete_flag'=>1);
		$this->db->update('member_category', $img, array('cat_id' => $id,'delete_flag'=>0));
		return $this->db->affected_rows();
	}
	public function deleteCatId($id = null){
		$this->db->update('cat_type', array('cat_type_delete_flag' => 1), array('cat_type_id' => $id,'cat_type_delete_flag'=>0));
		return $this->db->affected_rows();
	}
	public function updateCatId($id = null,$text = null){
		$this->db->update('cat_type', array('cat_type_name' => $text), array('cat_type_id' => $id));
		return $this->db->affected_rows();
	}
	public function deleteCatSubId($id = null){
		$this->db->update('cat_type_sub', array('cat_type_sub_delete_flag' => 1), array('cat_type_sub_id' => $id,'cat_type_sub_delete_flag'=>0));
		return $this->db->affected_rows();
	}
	public function updateCatSubId($id = null,$text = null){
		$this->db->update('cat_type_sub', array('cat_type_sub_name' => $text), array('cat_type_sub_id' => $id));
		return $this->db->affected_rows();
	}
	public function getMember(){
		$this->db->select('id,user_id, pass_id,email, phone,address');
//		$this->db->where(array('delete_flag'=>0));
		$data = $this->db->get('member')->result_array();
		return $data;
	}
	public function updateMemberPass($id = null,$text = null){
		$this->db->update('member', array('pass_id' => $text), array('id' => $id));
		return $this->db->affected_rows();
	}
	public function updateMemberDeny($id = null){
		$this->db->update('member', array('delete_flag' => 1), array('id' => $id));
		return $this->db->affected_rows();
	}
	public function updateMemberAllow($id = null){
		$this->db->update('member', array('delete_flag' => 0), array('id' => $id));
		return $this->db->affected_rows();
	}
	public function getHomeImage(){
		$this->db->select('id,images, link, type, name');
		$this->db->where(array('delete_flag'=>0));
		$data = $this->db->get('home')->result_array();
		return $data;
	}
	public function updateImgHome($data = null){
		$id = $data['id'];
		$img = array('images'=>$data['images'],'delete_flag'=>$data['delete_flag']);
		$this->db->update('home', $img, array('id' => $id));
		return $this->db->affected_rows();
	}
	public function checkExistCat($name=null){
		$this->db->select('cat_type_id, cat_type_name');
		$this->db->where(array('cat_type_delete_flag'=>0,'cat_type_name'=>$name));
		$data = $this->db->get('cat_type')->num_rows();
		return $data;
	}
	public function checkExistSubcat($name=null, $type = null){
		$this->db->select('cat_type_sub_id, cat_type_sub_name');
		$this->db->where(array('cat_type_sub_delete_flag'=>0,'cat_type_sub_name'=>$name,'cat_type_id'=>$type));
		$data = $this->db->get('cat_type_sub')->num_rows();
		return $data;
	}
}